﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Data;
using ProgNetComponents.Utils.EventHandlers;
using ProgNetComponents.Validation.Rules;
using ProgNetComponentsDx.Validation.Design;
using ProgNetComponents.Design.Editors;

namespace ProgNetComponentsDx.Validation
{
    public partial class ValidationComponent : Component
    {
        public ValidationComponent()
        {
            InitializeComponent();
            Rules = new ValidationRulesForTableCollection();
        }

        public ValidationComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            Rules = new ValidationRulesForTableCollection();
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(ValidationRulesForTableCollectionEditor), typeof(UITypeEditor))]
        public ValidationRulesForTableCollection Rules
        {
            get;
            private set;
        }

        [Editor(typeof(DataSetEditor), typeof(UITypeEditor))]
        public DataSet DataSource
        {
            get;
            set;
        }
        [Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor))]
        public string DataMember
        {
            get;
            set;
        }

        public event DataRowEventHandler ValidationPassed;
        public event DataRowEventHandler ValidationFailed;

        public bool StopOnFirstError
        {
            get;
            set;
        }

        protected virtual void OnValidationPassed(ValidationRule rule, DataRow row)
        {
            if (ValidationPassed != null)
                ValidationPassed(rule, new DataRowEventArgs(row));
        }
        protected virtual void OnValidationFailed(ValidationRule rule, DataRow row)
        {
            if (ValidationFailed != null)
                ValidationFailed(rule, new DataRowEventArgs(row));
        }

        public virtual bool ValidateAll()
        {
            bool result = true;
            {
                result = Rules.ValidateAll(StopOnFirstError);
            }
            return result;
        }

    }
}
